package com.yun.util.yun.utils.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * 压缩文件操作共计
 */
public class RarUtils {

    private static Logger logger = LoggerFactory.getLogger(RarUtils.class);

    /**
     * 接收并解压
     * @param fileStream
     * @throws Exception
     */
    public static void  decompressionFile(String outPath,InputStream fileStream) throws Exception {

        logger.debug("解压路径：{}",outPath);
        File file=new File(outPath);
        file.mkdir();
        //判断输出路径是否合法
        if (new File(outPath).isDirectory()) {
            if (!outPath.endsWith(File.separator)) {
                outPath += File.separator;
            }
            //zip读取压缩文件
            ZipInputStream zipInputStream = new ZipInputStream(fileStream, Charset.forName("GBK"));
            //解压文件
            decompressionFile(outPath, zipInputStream);
            //关闭流
            zipInputStream.close();
            fileStream.close();
        } else {
            throw new RuntimeException("输出路径不合法!");
        }
    }

    /**
     * ZipInputStream是逐个目录进行读取，所以只需要循环
     * @param outPath
     * @param inputStream
     * @throws IOException
     */
    private static void decompressionFile(String outPath, ZipInputStream inputStream) throws IOException {
        //读取一个目录
        ZipEntry nextEntry = inputStream.getNextEntry();
        //不为空进入循环
        while (nextEntry != null) {
            String name = nextEntry.getName();
            File file = new File(outPath+name);
            //如果是目录，创建目录
            if (name.endsWith("/")) {
                file.mkdir();
            } else {
                //文件则写入具体的路径中
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                int n;
                byte[] bytes = new byte[1024];
                while ((n = inputStream.read(bytes)) != -1) {
                    bufferedOutputStream.write(bytes, 0, n);
                }
                //关闭流
                bufferedOutputStream.close();
                fileOutputStream.close();
            }
            //关闭当前布姆
            inputStream.closeEntry();
            //读取下一个目录，作为循环条件
            nextEntry = inputStream.getNextEntry();
        }
    }

    /**
     * 给定根目录，返回一个相对路径所对应的实际文件名.
     * @param baseDir 指定根目录
     * @param absFileName 相对路径名，来自于ZipEntry中的name
     * @return java.io.File 实际的文件
     */
    public static File getRealFileName(String baseDir, String absFileName){
        logger.info("获取真实文件");
        String[] dirs=absFileName.split("/");
        logger.info("获取真实文件-1={},s={}",dirs.length,dirs.toString());
        File ret=new File(baseDir);
        logger.info("读取路径ret.exists={}",ret.exists());
        if(dirs.length>1)
        {
            logger.debug("dirs.length={}",dirs.length);
            for (int i = 0; i < dirs.length-1;i++)
            {
                ret=new File(ret, dirs[i]);
            }
            if(!ret.exists())
                ret.mkdirs();
            logger.info("ret");
            ret=new File(ret, dirs[dirs.length-1]);
            return ret;
        }
        logger.debug("ret.toString()={}",ret.toString());
        return ret;
    }

    public static void main(String[] arg){


    }
}
